package com.shopAuth.service.impl;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shopAuth.bean.AppSession;
import com.shopAuth.dao.AppSessionMapper;
import com.shopAuth.service.IAppSessionService;

import shopBean.common.bean.RestResult;
import shopBean.common.utils.DateCommonUtils;
import shopBean.common.utils.RestResultGenerator;
import shopBean.common.utils.Tool;
import shopBean.common.utils.UUIDGenerator;

@Service("appSessionService")
public class AppSessionServiceImpl implements IAppSessionService {

	@Autowired
	AppSessionMapper appSessionDao;
	private static Logger logger = LoggerFactory.getLogger(AppSessionServiceImpl.class);
	
	public RestResult authSession( String token) {
		AppSession appSession = appSessionDao.getByToken(token);
		if(appSession == null){
			return RestResultGenerator.genSuccessResult(false);
		}
		AppSession result = appSessionDao.getByUserId(appSession.getUserId());
		
		logger.info("获取用户得token:"+appSession.getToken() +"传入得token"+token);
		if(result.getStatus() ==1){
			return RestResultGenerator.genSuccessResult(false);
		}
		if(!result.getToken().equals(appSession.getToken())) {
			return RestResultGenerator.genSuccessResult(false);
		}
		RestResult returnResult = RestResultGenerator.genSuccessResult(true);
		returnResult.setData1(appSession.getUserId());
		return returnResult;
	}
	@Transactional
	public  RestResult createAppSession(String userId, String loginIp) {
		AppSession appSession = new AppSession();
		appSession.setCreateDate(DateCommonUtils.getCurrent());
		appSession.setLoginIp(loginIp);;
		appSession.setStatus(0);
		appSession.setUserId(userId);
		appSession.setLoginDate(DateCommonUtils.getCurrent());
		appSession.setUuid(UUIDGenerator.generate38());
		appSession.setToken(UUIDGenerator.generate38());
		appSessionDao.insert(appSession);
		return RestResultGenerator.genSuccessResult(appSession.getToken());
	}

}
